import 'package:flutter/material.dart';

enum HZBaseLoadingStatus { loading, noData, requestFailed, networkField }

class HZDefaultStatus extends StatelessWidget {
  final HZBaseLoadingStatus status;
  final Function onRetry;
  final Function onBack;

  const HZDefaultStatus({Key key, this.status, this.onRetry, this.onBack})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    Widget child = Text('其他');
    if (status == HZBaseLoadingStatus.loading) {
      child = HZBaseLoadingWaiting();
    }
    if (status == HZBaseLoadingStatus.requestFailed) {
      child = HZBaseLoadingFailed();
    }
    return Container(
      color: Colors.white,
      child: Center(
        child: child,
      ),
    );
  }
}

class HZBaseLoadingFailed extends StatelessWidget {
  final Function onRetry;
  final Function onBack;

  const HZBaseLoadingFailed({Key key, this.onRetry, this.onBack}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        SizedBox(
          width: 200,
          child: Image.asset(
            'assets/images/empty_page_image.png',
            fit: BoxFit.contain,
          ),
        ),
        Padding(
          padding: EdgeInsets.only(top: 10),
          child: Column(
            children: [
              HZBaseLoadingButton(
                title: '重新加载',
                onTap: onRetry,
              ),
              HZBaseLoadingButton(
                title: '返回',
                onTap: onBack,
              ),
            ],
          ),
        )
      ],
    );
  }
}

class HZBaseLoadingNoData extends StatelessWidget {
  final Function onRetry;

  const HZBaseLoadingNoData({Key key, this.onRetry}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        SizedBox(
          width: 200,
          child: Image.asset(
            'assets/images/empty_page_image.png',
            fit: BoxFit.contain,
          ),
        ),
        Padding(
          padding: EdgeInsets.only(top: 10),
          child: Column(
            children: [
              HZBaseLoadingButton(
                title: '重新加载',
                onTap: onRetry,
              ),
            ],
          ),
        )
      ],
    );
  }
}

class HZBaseLoadingWaiting extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        SizedBox(
          width: 35,
          height: 35,
          child: Image.asset('assets/images/loading1.webp'),
        ),
        Padding(
          padding: EdgeInsets.only(top: 20),
          child: Text(
            '加载中...',
            style: TextStyle(
              fontSize: 15,
              color: Colors.black,
            ),
          ),
        )
      ],
    );
  }
}

class HZBaseLoadingButton extends StatelessWidget {
  final String title;
  final Function onTap;

  const HZBaseLoadingButton({Key key, this.title, this.onTap})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return ConstrainedBox(
      constraints: BoxConstraints(minWidth: 120),
      child: OutlinedButton(
        onPressed: onTap,
        style: OutlinedButton.styleFrom(
          side: BorderSide(
            color: Colors.blue,
          ),
        ),
        child: Text(
          title,
          style: TextStyle(
            fontSize: 16,
            fontWeight: FontWeight.normal,
            color: Colors.blue,
          ),
        ),
      ),
    );
  }
}
